//
//  TiyonOrderCell.m
//  xpkc
//
//  Created by 胡志飞 on 15/12/23.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "TiyonOrderCell.h"

@implementation TiyonOrderCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createPayUI];
        
    }
    
    return  self;
    
}

-(void)createPayUI{
    
    // 间隔视图
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
    [self.headView setBackgroundColor:RGBCOLOR(231, 236, 237)];
    [self addSubview:self.headView];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 25, 80, 25)];
    [self.titleLabel setTextAlignment:NSTextAlignmentLeft];
    [self.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [self.contentView addSubview:self.titleLabel];
    
    self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-230, 30, 250, 25)];
    [self.numberLabel setTextAlignment:NSTextAlignmentCenter];
    [self.numberLabel setFont:[UIFont systemFontOfSize:12]];
    [self.numberLabel setTextColor:[UIColor darkGrayColor]];
    [self.contentView addSubview:self.numberLabel];
    
    
    self.bigView = [[UIImageView alloc] init];
    //    [self.bigView setBackgroundColor:RGBCOLOR(231, 236, 237)];

    [self.contentView addSubview:self.bigView];
    
    // 你点击起点过去到选择地址界面（在这里你点击这个是起点按钮或者终点按钮你就做个标记回去就好判断是哪一个按钮赋值了），选择地址，选择后跳转回来，你现在就可以判断前面的标记是起点，还是终点，是起点就给起点赋值，是终点就给终点赋值，不要告诉我你还没看没明白
    
    self.priceTit = [[UILabel alloc] init];
    [self.priceTit setTextAlignment:NSTextAlignmentCenter];
    [self.priceTit setTextColor:[UIColor darkGrayColor]];
    [self.priceTit setFont:[UIFont systemFontOfSize:14]];
    [self.contentView addSubview:self.priceTit];
    
    
    
    self.priceLabel = [[UILabel alloc] init];
    [self.priceLabel setTextAlignment:NSTextAlignmentCenter];
    [self.priceLabel setTextColor:[UIColor redColor]];
    [self.priceLabel setFont:[UIFont systemFontOfSize:14]];
    [self.contentView addSubview:self.priceLabel];
    
    
    self.payButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.payButton.layer.cornerRadius = 2;
    self.payButton.layer.masksToBounds = YES;
    //[ self.payButton setFrame:CGRectMake(SCREEN_WIDTH-100, 140, 80, 20)];
    [ self.payButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.contentView addSubview: self.payButton];
    
    self.chexiaoButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.chexiaoButton.layer.cornerRadius = 2;
    self.chexiaoButton.layer.masksToBounds = YES;
    self.chexiaoButton.layer.borderColor=[UIColor grayColor].CGColor;
    self.chexiaoButton.layer.borderWidth=1;
    [self.chexiaoButton setTitle:@"撤销订单" forState:(UIControlStateNormal)];
    [self.chexiaoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.contentView addSubview: self.chexiaoButton];
    
}

-(void)configWithOrderModel:(orderlistModel *)orderModel{
    
    self.numberLabel.text = [NSString stringWithFormat:@"订单号：%@",orderModel.oid];
    
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",orderModel.priceCount];
    
    if ([orderModel.state isEqual:@4]) {

        self.titleLabel.text = @"已完成";
        self.priceTit.text = @"已付款";
         [self.payButton setTitle:@"去评价" forState:(UIControlStateNormal)];
        [self.payButton setBackgroundColor:OrangeColor];
        
    }else{
        self.titleLabel.text =@"待支付";
        self.priceTit.text = @"应付款";
        [self.payButton setTitle:@"去付款" forState:(UIControlStateNormal)];
        [self.payButton setBackgroundColor:[UIColor redColor]];
    }
    
    NSArray * views = self.bigView.subviews;
    
    for (UIView * view in views) {
        [view removeFromSuperview];
    }
    
    CGFloat oneOrderH = 60;
    
    int arrayCount = orderModel.course.count;
    
    self.bigView.frame = CGRectMake(0, 65, SCREEN_WIDTH, oneOrderH * arrayCount);
    

    [self.bottomLineLabel setFrame:CGRectMake(20, 60 + arrayCount * oneOrderH, SCREEN_WIDTH-20, 2)];
    
    int k = 0;
    
    UILabel *lineLabel = [[UILabel alloc] init];
    [lineLabel setFrame:CGRectMake(0, 2 + k * oneOrderH, SCREEN_WIDTH-20, 2)];
    [lineLabel setBackgroundColor:RGBCOLOR(231, 236, 237)];
    [self.bigView addSubview:lineLabel];
    
    for (NSDictionary * dic in orderModel.course) {

        UILabel * courseNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10 + k * oneOrderH, SCREEN_WIDTH-50, 30)];
        courseNameLabel.numberOfLines=0;
        courseNameLabel.adjustsFontSizeToFitWidth=YES;
        [courseNameLabel setTextAlignment:NSTextAlignmentLeft];
        [courseNameLabel setTextAlignment:NSTextAlignmentLeft];
        [courseNameLabel setTextColor:[UIColor blackColor]];
        [courseNameLabel setFont:[UIFont systemFontOfSize:15]];
        
        [self.bigView addSubview:courseNameLabel];
        
        UILabel * subCourerLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 40+ k * oneOrderH, SCREEN_WIDTH-75, 20)];
        subCourerLabel.numberOfLines=0;
        subCourerLabel.adjustsFontSizeToFitWidth=YES;
        [subCourerLabel setTextAlignment:NSTextAlignmentLeft];
        [subCourerLabel setTextColor:[UIColor darkGrayColor]];
        [subCourerLabel setFont:[UIFont systemFontOfSize:13]];
        
        [self.bigView addSubview:subCourerLabel];
        
        UIImageView * freeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 42+ k * oneOrderH, 15, 15)];
        [self.bigView addSubview:freeImageView];
        
        NSLog(@"dic==>%@",dic);
        
        if ([[NSString stringWithFormat:@"%@",[dic objectForKey:@"is_gift"]] isEqualToString:@"0"]) {

            courseNameLabel.text=[NSString stringWithFormat:@"%@",[dic objectForKey:@"title"]];
        }

        if ([[NSString stringWithFormat:@"%@",[dic objectForKey:@"is_gift"]] isEqualToString:@"1"]) {
            subCourerLabel.text = [dic objectForKey:@"title"];
            [freeImageView setImage:[UIImage imageNamed:@"freeCourse.png"]];

        }
        
        if (k > 0) {
            
            UILabel *blineLabel = [[UILabel alloc] init];
            [blineLabel setFrame:CGRectMake(20, 2+ 55+ k * oneOrderH -1, SCREEN_WIDTH-20, 2)];
            
            [blineLabel setBackgroundColor:RGBCOLOR(231, 236, 237)];
            [self.bigView addSubview:blineLabel];
        }
       
        k ++;

    }
    
    self.priceTit.frame = CGRectMake(20, 140 + oneOrderH * arrayCount-40, 50, 20);
    self.priceLabel.frame = CGRectMake(50, 140  + oneOrderH * arrayCount-40, 100, 20);
    [ self.payButton setFrame:CGRectMake(SCREEN_WIDTH-100, 140 + oneOrderH * arrayCount-40, 80, 20)];
    [self.chexiaoButton setFrame:CGRectMake(SCREEN_WIDTH-200, 140 + oneOrderH * arrayCount-40, 80, 20)];
}

@end
